package com.lanou.musicproject.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;

import android.graphics.drawable.BitmapDrawable;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.musicproject.R;
import com.lanou.musicproject.bean.MySong;
import com.lanou.musicproject.musicservice.MusicInterface;
import com.lanou.musicproject.musicservice.MusicService;
import com.lanou.musicproject.suspendservice.FxService;
import com.lanou.musicproject.utils.MusicPlayer;
import com.lanou.musicproject.utils.MyConfig;
import com.lanou.musicproject.utils.ReadDataFromContentProvider;

import java.io.File;
import java.io.FileDescriptor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;

/**
 * Created by ztq on 15/10/23.
 */
public class ScanActivity extends Activity implements AdapterView.OnItemClickListener {
    private static MusicInterface mi;
    private ListView listView;

    public static List<MySong> data;


    private RelativeLayout root;
    private AudioAdapter adapter;
    private MusicPlayer musicPlayer;
    private MyServiceCon connection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);


        root = (RelativeLayout) findViewById(R.id.root);

        data = ReadDataFromContentProvider.readAudio(this);

        if (data == null || data.size() == 0) {
            Toast.makeText(this, "没有扫描到文件", Toast.LENGTH_SHORT).show();
            return;
        }

        listView = (ListView) findViewById(R.id.listview);
        adapter = new AudioAdapter(this, data);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);


        ReadDataFromContentProvider.readAudio(this);


        Intent intent = new Intent(ScanActivity.this, MusicService.class);
        startService(intent);

        connection = new MyServiceCon();
        bindService(intent, connection, BIND_AUTO_CREATE);





    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

        Bitmap bmp = data.get(position).getAlbumCover();
        if (bmp != null) {
            BitmapDrawable drawable = new BitmapDrawable(getResources(), bmp);
            root.setBackgroundDrawable(drawable);
        }
    }


    class AudioAdapter extends BaseAdapter {

        private Context context;
        private List<MySong> data;

        public AudioAdapter(Context context, List<MySong> data) {
            this.context = context;
            this.data = data;
        }

        @Override
        public int getCount() {

            int count = data.size();
            //通过广播传出扫描的数目
            Intent intent = new Intent();
            intent.setAction("countAction");
            intent.putExtra("countLocal",count);
            context.sendBroadcast(intent);

            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            View view;
            if (convertView != null) {
                view = convertView;
            } else {
                view = LayoutInflater.from(context).inflate(R.layout.scan_list_item, null);
            }

            TextView song_name = (TextView) view.findViewById(R.id.song_name);
            song_name.setText(data.get(position).getTitle());
            TextView singer_name = (TextView) view.findViewById(R.id.singer_name);
            singer_name.setText(data.get(position).getArtist() + "\t" + data.get(position).getAlbum());
            ImageButton local_play = (ImageButton) view.findViewById(R.id.local_play);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ArrayList<MySong> list = new ArrayList<>();
                    for (MySong m : data) {
                        MySong mySong = new MySong();
                        mySong.setTitle(m.getTitle());//歌名
                        mySong.setDuration(m.getDuration());//时长
                        mySong.setArtist(m.getArtist());//歌手
                        mySong.setAlbum(m.getAlbum());//专辑
                        mySong.setPath(m.getPath());//路径
                        mySong.setLrclink(m.getLrclink());//歌词网址



                        list.add(mySong);
                    }

                    MainActivity.mini_play(list, position);
                    FxService.mini_play(list,position);
                    MusicPlayActivity.playIv(list,position);
                }
            });


            //本地播放方法
            local_play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    switch (v.getId()){
                        case R.id.local_play:

                            if (mi.getIsPlay()) {
                                mi.pause();
                            } else  {
                                Log.i("---play---","123");
                                mi.playLocal(data,position);
                            }
                            break;
                    }
                }
            });
            return view;
        }
    }

    class MyServiceCon implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mi = (MusicInterface) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }
}
